﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace LexicalAnalysisTest
{
    class Program
    {
        /*
         * 编写一个计算器，要求和Ｗｉｎｄｏｗｓ系统自带计算器基本相同的功能。
         * 能实现简单运算，如12+5=17；混合运算，如12+6*7=54,15-2*8+6/2等等；带等号运算，如5+2=7*8=56-4*8=23等。
         * 要求输入过程不能显示出来。
         */

        private static readonly char[] Digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        private static readonly char[] Operators = { '+', '-', '*', '/' };

        private static List<string> LexicalAnlysis(string sEquation)
        {
            if (string.IsNullOrEmpty(sEquation))
                return null;

            var list = new List<string>(20);
            for (int i = 0, l = sEquation.Length; i < l; i++)
            {
                var token = string.Empty;
                var c = sEquation[i];

                if (i == l - 1)
                {
                    list.Add(c.ToString());
                    return list;
                }



            }
        }

        private static long? Compute(string sEquation)
        {
            var tokens = LexicalAnlysis(sEquation);
            if (null == tokens || 0 == tokens.Count)
                return null;

            foreach (var c in sEquation)
            {

            }

            return 1;
        }

        private static string ReadEquation(bool showKey = false)
        {
            var sEquation = string.Empty;
            while (true)
            {
                var key = Console.ReadKey(showKey);

                if (key.Key == ConsoleKey.Escape)
                    return null;

                if (key.Key == ConsoleKey.Enter || key.KeyChar == '=')
                    return sEquation;

                if (Digits.Any(it => it == key.KeyChar) || Operators.Any(it => it == key.KeyChar))
                    sEquation += key.KeyChar;
            }
        }

        static void Main(string[] args)
        {
            //false: show keys; true: NOT show
            var showKey = false;
            while (true)
            {
                Console.Write("Enter the equation comprise of 0-9,+,-,* and /, hit ESC to stop:");
                var s = ReadEquation(showKey);
                if (null == s)
                    return;

                Console.WriteLine();
                var result = Compute(s);
                if (null != result)
                    Console.WriteLine(result.Value);
            }
        }
    }
}
